package com.sojpt.boot.controller.business.validator;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.validate.Validator;
import com.sojpt.constant.Const;
import com.sojpt.model.DicUser;

/**
 * Description: 本类用于学生注册
 * Created by JYN_LL on 2020/2/13 9:24
 */
public class LkyStudentDuplicateValidator extends Validator {
    @Override
    protected void validate(Controller controller) {
        DicUser dicUser= controller.getModel(DicUser.class,"student");
        System.out.println(dicUser.toString());
        if(dicUser.getId() == null){
            UserNameDuplicate(dicUser);
        }

    }

    @Override
    protected void handleError(Controller controller) {
        controller.renderJson();
    }

    public void UserNameDuplicate(DicUser dicUser) {

        SqlPara sqlPara = Db.getSqlPara("lku_registerStudent.selectByUserCode", dicUser.getUserCode());
        if(sqlPara==null){
            return;
        }
        Record r = Db.findFirst(sqlPara);

        SqlPara sqlPara1=Db.getSqlPara("lku_registerStudent.selectByIdCard", dicUser.getIdCard());
        Record byIdCardRecord = Db.findFirst(sqlPara);

        if (r != null || Const.rootName.equals(dicUser.getUserCode())) {
            addError("msg", "该学号已存在!");
        }
        if (byIdCardRecord != null ) {
            addError("msg", "该身份证号已存在!");
        }
    }
}
